*****Conscription and Military Service: Do They Result in Future Violent and Non-Violent Incarcerations and Recidivism?  Wang, Flores-Lagunes 2020*****
ssc install estout
set more off
cd "C:\Users\xinto\Dropbox\Vietnam Veteran Paper\Crime Paper\2020 JHR Data\1. Main Dataset\Main SISFCF Data Set" 

use birthpop_06232017, clear

rename whitebirth birth1
rename nonwhitebirth birth0

rename birthmonth monthBirth1 
rename BirthMonth monthBirth0

reshape long birth monthBirth, i(by bm bd lott eligible) j(white)

egen monthPop=max(monthBirth), by(by bm white)

rename by birthYear
rename bm birthMonth 
rename bd birthDay  

gen drawingYear = 69 if birthYear>=44 & birthYear<=50
replace drawingYear = 70 if birthYear==51
replace drawingYear = 71 if birthYear==52
replace drawingYear = 72 if birthYear==53
drop if drawingYear==.
  
gen APNheight = 1
replace APNheight = 195 if drawingYear==69
replace APNheight = 125 if drawingYear==70
replace APNheight = 95  if drawingYear==71
replace APNheight = 95  if drawingYear==72

gen ygroup1  = birthYear>=48 & birthYear<=52
gen ygroup2  = birthYear>=48 & birthYear<=50
gen ygroup3  = birthYear==50
gen ygroup4  = birthYear==51 
gen ygroup5  = birthYear==52 
gen ygroup6  = birthYear==53

**Generate birth season by year dummies 
gen seasonYear=999
tab birthMonth
  forvalues i=0(1)3 {

    replace seasonYear=`i'+1 if birthMonth>3*`i' & birthMonth<=3*(`i'+1)

    }
tab seasonYear

**Compute the white and nonwhite males born in each season by year group 
egen seasonPop=total(birth), by(white seasonYear)

egen seasonYear_group=group(seasonYear birthYear) 
tab seasonYear_group, gen(seasonYear_group) 

**Generate birth month by year dummies 
egen monthYear_group=group(birthMonth birthYear) 
tab monthYear_group, gen(monthYear_group) 

sort birthYear birthMonth birthDay white

save vsus_06242017, replace


use inmate_data, clear 

***Separate the inmate sample into 3 survey years, and merge the VSUS sample one by one
*1979 survey  
keep if study==1979
save inmate_sample_1979, replace 
*1986 survey 
use inmate_data, clear 
keep if study==1986
save inmate_sample_1986, replace 
*1991 survey 
use inmate_data, clear 
keep if study>=1991
save inmate_sample_1991, replace 

***Merge the inmate sample to the birth VSUS sample 
*Merge 1979 survey
use vsus_06242017, clear
drop Check Check2 monthBirth

merge 1:m birthYear birthMonth birthDay white using inmate_sample_1979, gen(inmate)
drop if inmate==1
replace inmate=1 if inmate==2 | inmate==3 

save vsus_inmate_sample_1979, replace

*Merge 1986 survey
use vsus_06242017, clear
drop Check Check2 monthBirth

merge 1:m birthYear birthMonth birthDay white using inmate_sample_1986, gen(inmate)
drop if inmate==1
replace inmate=1 if inmate==2 | inmate==3 

save vsus_inmate_sample_1986, replace

*Merge 1991 survey
use vsus_06242017, clear
drop Check Check2 monthBirth

merge 1:m birthYear birthMonth birthDay white using inmate_sample_1991, gen(inmate)
drop if inmate==1
replace inmate=1 if inmate==2 | inmate==3 

save vsus_inmate_sample_1991, replace

***Create a varaible Census in VSUS, 
**fill each observation in VSUS with a strata id from 2-_N 
use vsus_06242017, clear
drop Check Check2 monthBirth
gen inmate=0 
egen Census=fill(2 3 4) 
save vsus_2017, replace

***Append all the surveys back on each other 
use vsus_inmate_sample_1979, clear 
append using vsus_inmate_sample_1986 
append using vsus_inmate_sample_1991 
append using vsus_2017

gen lindo=0 

save vsus_inmate_sample, replace

/***Checking the codes***
use vsus_inmate_sample, clear
    
*Count how many distinct birth months are there 
	distinct monthYear_group
	local total_my=r(ndistinct) 
	
	*Generate new variable numering the birth month year group in the subsample
	tempvar new_my
	egen `new_my'=group(monthYear_group) 
	tab `new_my'

	keep if monthYear_group!=.
	
    collapse (mean) monthYear_group birth eligible `new_my', by(birthYear birthMonth birthDay white)
	
	tempvar wn_birth 
	egen wn_birth=total(birth), by(white eligible birthMonth birthYear)
	tab wn_birth
	
	di "`x'"
	
	sum wn_birth if eligible==1 
    sca nz1`x'=r(mean) 
    sca li nz1`x' 

    sum wn_birth if  eligible==0 
    sca nz0`x'=r(mean) 
    sca li nz0`x' 
*/ 

***Check Lindo and Stoecker's data 
use lindo_1979_2, clear
	append using lindo_1986_2 
	append using lindo_1991_2 
	
global outcomescount = "criminal violent nonviolent violent_desistence nonviolent_desistence"	
global mainoutcomes1 = "criminal1 violent1 nonviolent1 violent_desistence1 nonviolent_desistence1"

foreach var of varlist $outcomescount {
      replace `var' = . if `var' == 9
    }	

collapse (sum)  $outcomescount ///
             (mean) mob dob birth ygroup* fe_yob* yobmob fe_mob* drawingYear induction yobRSNID ///
                    relativeLotteryNumber relLotNumInteract relLotNumSquared relLotNumSqInter yeargroup ///
                    relLotNumCubed  relLotNumCuInter staylength, by(yob lotteryNumber white study)
					
	foreach var of varlist $outcomescount {
      gen `var'1 = `var'/birth
    }		
gen birth_weight=round(birth, 0.0001)

**Step 1: test to see whether all the 2sls estimates are all accurate
   *1979 survey.  
   # delimit cr 
   set more off
  local stage1_grp2_white1=0.112
  local stage1_grp2_white0=0.072 
  
  * Reduced-Form Results (deleted fe survey)
	local options= "append drop(fe_* _cons, relax) collabels(,none) style(tex) label cells(b(fmt(%9.5f) star) se(par fmt(%9.5f))) starlevels(* .10 ** .05 *** .01) stats(pctimpact veteffect N, fmt(%9.1f %9.5f %9.0f) labels(%Impact VeteranEffectPP N))"
	
	tab birth_weight
	foreach outcome in $mainoutcomes1 {
       display "`outcome'"
		foreach race in 1 0 { 
			foreach yeargroup of numlist  2 { 
			    xtset yobmob
				eststo, title("`ygroupl`yeargroup''"): xtreg `outcome' induction if ygroup`yeargroup'==1 & white==`race' & study==1979 [aweight=birth_weight], fe
				sum `outcome' if induction==0 & e(sample)
				matrix temp = e(b)
				estadd scalar pctimpact = 100*temp[1,1]/r(mean)
				if (`yeargroup'~=1) { 
				estadd scalar veteffect = (temp[1,1])/`stage1_grp2_white`race''
				}
			}
			}
			}

			
	*1986 survey.  
   # delimit cr 
   set more off
  local stage1_grp2_white1=0.112
  local stage1_grp2_white0=0.072 
  
  * Reduced-Form Results (deleted fe survey)
	local options= "append drop(fe_* _cons, relax) collabels(,none) style(tex) label cells(b(fmt(%9.5f) star) se(par fmt(%9.5f))) starlevels(* .10 ** .05 *** .01) stats(pctimpact veteffect N, fmt(%9.1f %9.5f %9.0f) labels(%Impact VeteranEffectPP N))"
	
	tab birth_weight
	foreach outcome in $mainoutcomes1 {
       display "`outcome'"
		foreach race in 1 0 {		     
			foreach yeargroup of numlist  2 { 
			    xtset yobmob
				eststo, title("`ygroupl`yeargroup''"): xtreg `outcome' induction if ygroup`yeargroup'==1 & white==`race' & study==1986 [aweight=birth_weight], fe
				sum `outcome' if induction==0 & e(sample)
				matrix temp = e(b)
				estadd scalar pctimpact = 100*temp[1,1]/r(mean)
				if (`yeargroup'~=1) { 
				estadd scalar veteffect = (temp[1,1])/`stage1_grp2_white`race''
				}
			}
			}
			}		
			
			
	*1991 survey.  
   # delimit cr 
   set more off
  local stage1_grp2_white1=0.112
  local stage1_grp2_white0=0.072 
  
  * Reduced-Form Results (deleted fe survey)
	local options= "append drop(fe_* _cons, relax) collabels(,none) style(tex) label cells(b(fmt(%9.5f) star) se(par fmt(%9.5f))) starlevels(* .10 ** .05 *** .01) stats(pctimpact veteffect N, fmt(%9.1f %9.5f %9.0f) labels(%Impact VeteranEffectPP N))"
	
	tab birth_weight
	foreach outcome in $mainoutcomes1 {
       display "`outcome'"
		foreach race in 1 0 {
			foreach yeargroup of numlist  2 { 
			    xtset yobmob
				eststo, title("`ygroupl`yeargroup''"): xtreg `outcome' induction if ygroup`yeargroup'==1 & white==`race' & study==1991 [aweight=birth_weight], fe
				sum `outcome' if induction==0 & e(sample)
				matrix temp = e(b)
				estadd scalar pctimpact = 100*temp[1,1]/r(mean)
				if (`yeargroup'~=1) { 
				estadd scalar veteffect = (temp[1,1])/`stage1_grp2_white`race''
				}
			}
			}
			}		
			
			
   *All survey.  
   # delimit cr 
   set more off
   
  local stage1_grp2_white1=0.112
  local stage1_grp2_white0=0.072 
  
  * Reduced-Form Results (deleted fe survey)
	local options= "append drop(fe_* _cons, relax) collabels(,none) style(tex) label cells(b(fmt(%9.5f) star) se(par fmt(%9.5f))) starlevels(* .10 ** .05 *** .01) stats(pctimpact veteffect N, fmt(%9.1f %9.5f %9.0f) labels(%Impact VeteranEffectPP N))"
	
	tab birth_weight
	foreach outcome in $mainoutcomes1 {
       display "`outcome'"
		foreach race in 1 0 {
			foreach yeargroup of numlist  2 { 
			    xtset yobmob 
				eststo, title("`ygroupl`yeargroup''"): xtreg `outcome' induction study  if ygroup`yeargroup'==1 & white==`race' [aweight=birth_weight], fe
				sum `outcome' if induction==0 & e(sample)
				matrix temp = e(b)
				estadd scalar pctimpact = 100*temp[1,1]/r(mean)
				if (`yeargroup'~=1) { 
				estadd scalar veteffect = (temp[1,1])/`stage1_grp2_white`race''
				}
			}
			}
			}			
		

**Step 2: Append Lindo's data to the incarceration paper data 
use lindo_1979_2, clear
	append using lindo_1986_2 
	append using lindo_1991_2 

global outcomescount = "violent_desistence nonviolent_desistence"	
global mainoutcomes1 = "violent_desistence1 nonviolent_desistence1"

/*Unify the definition of year groups
ygroup1 for birth year in 1948-1952 
ygroup2 for birth year in 1948-1950
ygroup3 for birth year in 1950
ygroup4 for birth year in 1951 
ygroup5 for birth year in 1952
ygroup6 for birth year in 1951-1952
ygroup7 for birth year in 1950-1952
*/
drop ygroup*

/* Unifying Lindo's year groups with mine */
gen ygroup1=1 if yob>=1948 & yob<=1952
replace ygroup1=0 if ygroup1==. 

gen ygroup2=1 if yob>=1948 & yob<=1950
replace ygroup2=0 if ygroup2==.

gen ygroup3=1 if yob==1950
replace ygroup3=0 if ygroup3==.

gen ygroup4=1 if yob==1951
replace ygroup4=0 if ygroup4==.

gen ygroup5=1 if yob==1952
replace ygroup5=0 if ygroup5==.

gen ygroup6=1 if yob>=1951 & yob<=1952
replace ygroup6=0 if ygroup6==.

gen ygroup7=1 if yob>=1950 & yob<=1952
replace ygroup7=0 if ygroup7==.

gen ygroup8=1 if yob==1953
replace ygroup8=0 if ygroup8==.

foreach var of varlist $outcomescount {
      replace `var' = . if `var' == 9
    }		
 
gen lindo=1 
gen Census=-1
tab study, gen (survey)
save lindo_all, replace 

use vsus_inmate_sample, clear

replace totalSample=1 if birthYear==53 & male==1 & vv!=9 & lott!=999 & inmate==1

drop ygroup*

gen ygroup1=1 if birthYear>=48 & birthYear<=52
replace ygroup1=0 if ygroup1==. 

gen ygroup2=1 if birthYear>=48 & birthYear<=50
replace ygroup2=0 if ygroup2==.

gen ygroup3=1 if birthYear==50
replace ygroup3=0 if ygroup3==.

gen ygroup4=1 if birthYear==51
replace ygroup4=0 if ygroup4==.

gen ygroup5=1 if birthYear==52
replace ygroup5=0 if ygroup5==.

gen ygroup6=1 if birthYear>=51 & birthYear<=52
replace ygroup6=0 if ygroup6==.

gen ygroup7=1 if birthYear>=50 & birthYear<=52
replace ygroup7=0 if ygroup7==.

gen ygroup8=1 if birthYear==53
replace ygroup8=0 if ygroup8==.

append using lindo_all 

**Generate birth season by year dummies 

tab birthMonth
  forvalues i=0(1)3 {

    replace seasonYear=`i'+1 if birthMonth>3*`i' & birthMonth<=3*(`i'+1)

    }
	
/**Compute the white and nonwhite males born in each season by year group 
egen seasonPop=total(birth), by(white seasonYear)

egen seasonYear_group=group(seasonYear birthYear) if ygroup1==1 
tab seasonYear_group if ygroup8==1

**Generate birth month by year dummies 
tab birthMonth if ygroup1==1
tab birthYear if ygroup1==1
egen monthYear_group=group(birthMonth birthYear) if ygroup1==1 
*/

tab monthYear_group if ygroup1==1

sort birthYear birthMonth birthDay white

save pop_sample_03162020, replace  

 



